package com.trinity.campus.service.impl;

import com.trinity.campus.domain.CampusSchoolOrg;
import com.trinity.campus.mapper.CampusSchoolOrgMapper;
import com.trinity.campus.service.ICampusSchoolOrgService;
import com.trinity.common.utils.DateUtils;
import com.trinity.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 学校组织机构Service业务层处理
 *
 * @author dsp
 * @date 2024-01-18
 */
@Service
public class CampusSchoolOrgServiceImpl implements ICampusSchoolOrgService {
    @Resource
    private CampusSchoolOrgMapper campusSchoolOrgMapper;

    /**
     * 查询学校组织机构
     *
     * @param id 学校组织机构主键
     * @return 学校组织机构
     */
    @Override
    public CampusSchoolOrg selectCampusSchoolOrgById(String id) {
        return campusSchoolOrgMapper.selectCampusSchoolOrgById(id);
    }

    /**
     * 查询学校组织机构列表
     *
     * @param campusSchoolOrg 学校组织机构
     * @return 学校组织机构
     */
    @Override
    public List<CampusSchoolOrg> selectCampusSchoolOrgList(CampusSchoolOrg campusSchoolOrg) {
        return campusSchoolOrgMapper.selectCampusSchoolOrgList(campusSchoolOrg);
    }

    public List<CampusSchoolOrg> buildOrgTree(List<CampusSchoolOrg> orgs) {
        List<CampusSchoolOrg> returnList = new ArrayList<>();
        List<String> tempList = orgs.stream().map(CampusSchoolOrg::getId).collect(Collectors.toList());
        for (CampusSchoolOrg org : orgs) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(org.getParentId())) {
                recursionFn(orgs, org);
                returnList.add(org);
            }
        }
        if (returnList.isEmpty()) {
            returnList = orgs;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<CampusSchoolOrg> list, CampusSchoolOrg t) {
        // 得到子节点列表
        List<CampusSchoolOrg> childList = getChildList(list, t);
        t.setChildren(childList);
        for (CampusSchoolOrg tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<CampusSchoolOrg> getChildList(List<CampusSchoolOrg> list, CampusSchoolOrg node) {
        List<CampusSchoolOrg> tlist = new ArrayList<>();
        for (CampusSchoolOrg n : list) {
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(node.getId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<CampusSchoolOrg> list, CampusSchoolOrg t) {
        return getChildList(list, t).size() > 0;
    }

    @Override
    public Map<String, List<CampusSchoolOrg>> schoolOrgOption(CampusSchoolOrg campusSchoolOrg) {
        // 定义要筛选的strucType值列表
        Set<String> strTypes = new HashSet<>(Arrays.asList("STRUC_CLASS", "STRUC_MAJOR", "STRUC_COLLEGE", "STRUC_SCHOOL"));

        List<CampusSchoolOrg> orgList = campusSchoolOrgMapper.selectCampusSchoolOrgList(campusSchoolOrg);

        return orgList.stream()
                .filter(org -> strTypes.contains(org.getStrucType()))
                .collect(Collectors.groupingBy(CampusSchoolOrg::getStrucType));
    }

    /**
     * 新增学校组织机构
     *
     * @param campusSchoolOrg 学校组织机构
     * @return 结果
     */
    @Override
    public int insertCampusSchoolOrg(CampusSchoolOrg campusSchoolOrg) {
        campusSchoolOrg.setCreateTime(DateUtils.getNowDate());
        return campusSchoolOrgMapper.insertCampusSchoolOrg(campusSchoolOrg);
    }

    /**
     * 修改学校组织机构
     *
     * @param campusSchoolOrg 学校组织机构
     * @return 结果
     */
    @Override
    public int updateCampusSchoolOrg(CampusSchoolOrg campusSchoolOrg) {
        campusSchoolOrg.setUpdateTime(DateUtils.getNowDate());
        return campusSchoolOrgMapper.updateCampusSchoolOrg(campusSchoolOrg);
    }

    /**
     * 批量删除学校组织机构
     *
     * @param ids 需要删除的学校组织机构主键
     * @return 结果
     */
    @Override
    public int deleteCampusSchoolOrgByIds(String[] ids) {
        return campusSchoolOrgMapper.deleteCampusSchoolOrgByIds(ids);
    }

    /**
     * 删除学校组织机构信息
     *
     * @param id 学校组织机构主键
     * @return 结果
     */
    @Override
    public int deleteCampusSchoolOrgById(String id) {
        return campusSchoolOrgMapper.deleteCampusSchoolOrgById(id);
    }
}
